<template>
	<view class="card-pages">
		<u-navbar
			title=""
			@rightClick="rightClick"
			rightIcon="list"
			:autoBack="true"
		></u-navbar>
		<!-- 用户信息部分 -->
		<view class="card-pages-user">
			<view class="user">
				<UserDataVue
					:iconWidth="34"
					:iconHeight="34"
					:avatarImg="author.avatar"
					:author="author"
				></UserDataVue>
			</view>
			<!-- 关注 -->
			<view
				class="follow followed"
				v-if="isFollow"
			>
				已关注
			</view>
			<view
				class="follow"
				v-if="!isFollow"
				@click="follow"
			>
				+关注
			</view>
		</view>
		<!-- 轮播图部分 -->
		<view class="card-img-swiper">
			<SwiperVue
				:indicator="true"
				:height="500"
				:allowClick="true"
				:imgList="cardDatas.images"
			></SwiperVue>
		</view>
		<view class="card-info">
			<view class="card-title">
				<span>{{ cardDatas.title }}</span>
			</view>
			<view class="card-content">
				{{ cardDatas.content }}
				<span class="labels">
					<span
						v-for="(tag, index) in tags"
						@click="JumpSearchTag(tag)"
						style="color: darkblue"
					>
						#{{ tag }}
					</span>
				</span>
			</view>
			<view class="card-times">
				<span>{{ cardDatas.createdAt }}</span>
			</view>
		</view>
		<view
			class="line"
			style="width: 100%; display: flex; justify-content: center; margin: 15rpx"
		>
			<u-line length="90%"></u-line>
		</view>
		<view class="card-comment-warp">
			<view class="card-comment">
				<view class="all-comment">
					<span>共{{ commentNum }}条评论</span>
				</view>
				<view class="input-comment">
					<view class="my-avatar">
						<u-avatar
							size="30"
							:src="userInfo.avatar"
						></u-avatar>
					</view>
					<u-search
						placeholder="我看你骨骼惊奇,一定有话要说吧"
						:disabled="true"
						search-icon="more-circle"
						actionText=""
						style="margin-left: 10px"
						@click="showMask"
					></u-search>
				</view>
				<view class="comments">
					<commentPageVue :comment="comment"></commentPageVue>
					<view class="loadMore">
						<span>——我也是有底线的~——</span>
					</view>
				</view>
			</view>
		</view>

		<view class="card-foot-warp">
			<view class="foot">
				<view class="foot-input">
					<u-search
						placeholder="评论一句吧~"
						:disabled="true"
						search-icon="more-circle"
						actionText=""
						:showAction="false"
						@click="showMask"
					></u-search>
				</view>
				<view
					class="foot-icon like"
					@click="addLike"
				>
					<i class="certishare-icon">
						<span
							v-if="isLike"
							style="color: red"
						>
							&#xe86f;
						</span>
						<span v-if="!isLike">&#xe86f;</span>
						<span class="icon-text">{{ cardDatas.likeCount }}</span>
					</i>
				</view>
				<view
					class="foot-icon collect"
					:class="{ collected: isColletc }"
					@click="addCollect"
				>
					<i class="certishare-icon">
						<span>&#xe604;</span>
					</i>
				</view>
				<view class="foot-icon comment">
					<i class="certishare-icon">
						<span>&#xe891;</span>
						<span class="icon-text">{{ commentNum }}</span>
					</i>
				</view>
			</view>
		</view>
		<commentBlockVue :cardDatas="cardDatas"></commentBlockVue>
		<view
			class="card-page-mask"
			v-if="show"
			@click="hideMask"
		></view>
		<view
			class="float-window"
			:style="{ top: floatWindowTop + 'px' }"
		>
			<span class="text">分享至</span>
			<view class="share-btns">
				<view class="share-btn">
					<view class="share-icon">
						<i class="certishare-icon">
							<span>&#xe841;</span>
						</i>
					</view>
					<view class="share-text">复制链接</view>
				</view>
			</view>
			<u-line></u-line>
			<view class="manage-btns">
				<view
					class="manage-btn"
					v-if="canDelete"
				>
					<view
						class="manage-icon"
						@click="deleteCard"
					>
						<i class="certishare-icon">
							<span>&#xe67e;</span>
						</i>
					</view>
					<view class="manage-text">删除</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import commentBlockVue from '../../components/comment-page/comment-block.vue';
import commentPageVue from '../../components/comment-page/comment-page.vue';
import UserDataVue from '../../components/index/index_card/UserData.vue';
import SwiperVue from '../../components/index/Swiper/Swiper.vue';
import { mapState } from 'vuex';
import { getCardInfo, setHistory, addLikeCard, addCollectCard, deleteCard, checkUserInteractions } from '@/common/api/cardApi.js';
export default {
	data() {
		return {
			show: false,
			floatWindowTop: 1000,
			cardDatas: {},
			author: {},
			content: '确定要删除此卡片吗',
			comment: [],
			tags: [],
			isFollow: false,
			isLike: false,
			isColletc: false
		};
	},
	components: {
		UserDataVue,
		SwiperVue,
		commentPageVue,
		commentBlockVue
	},
	computed: {
		...mapState({
			userInfo: (state) => state.user.userInfo
		}),

		canDelete() {
			if (this.author._id === this.userInfo.id) {
				return true;
			} else {
				return false;
			}
		},
		commentNum() {
			return this.comment.length;
		}
	},
	methods: {
		rightClick() {
			this.show = true;
			this.floatWindowTop = 700;
		},
		showMask() {
			uni.$emit('showMask');
		},
		hideMask() {
			this.show = false;
			this.floatWindowTop = 1000;
		},
		saveHistory(cardId) {
			const userId = this.userInfo.id;
			if (!userId) {
			} else {
				setHistory(userId, cardId).then((res) => {});
			}
		},
		follow() {},
		addLike() {
			//把卡片和用户的id推过去就行
			const userId = this.userInfo.id;
			const cardId = this.cardDatas._id;
			const params = {
				userId,
				cardId
			};
			addLikeCard(params).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					this.isLike = true;
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					this.isLike = false;
				}
			});
		},

		//这个函数用来查询是否关注了，是否喜欢，是否收藏了
		chekInfo(cardId) {
			const userId = this.userInfo.id;
			if (!userId) {
			} else {
				const params = {
					userId,
					cardId
				};
				checkUserInteractions(params).then((res) => {
					this.isFollow = res.isFollowingAuthor;
					this.isLike = res.isLiked;
					this.isColletc = res.isCollected;
				});
			}
		},
		addCollect() {
			const userId = this.userInfo.id;
			const cardId = this.cardDatas._id;
			const params = {
				userId,
				cardId
			};
			addCollectCard(params).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				}
			});
		},
		deleteCard() {
			//用户id和卡片id
			const userId = this.userInfo.id;
			const cardId = this.cardDatas._id;
			const params = {
				userId,
				cardId
			};
			uni.showModal({
				title: '提示',
				content: '确定要删除吗\n现在回头还来得及！',
				success: async function (res) {
					if (res.confirm) {
						uni.showLoading({
							title: '正在删除'
						});
						await deleteCard(params).then((res) => {
							uni.hideLoading();
							uni.navigateTo({
								url: '/pages/index/index'
							});
						});
					}
				}
			});
		},
		JumpSearchTag(key) {
			uni.navigateTo({
				url: `/pages/search-result-page/search-result-page?search=${key}`
			});
		}
	},
	onLoad(option) {
		const that = this;
		const id = option.id;
		getCardInfo(id)
			.then((res) => {
				that.cardDatas = res.cardData;
				that.author = res.author;
				that.comment = res.comments;
				that.tags = res.cardData.tags;
				console.log('评论信息', res.comments);
			})
			.catch((err) => {
				uni.showToast({
					title: '网络错误~',
					icon: 'none'
				});
			});
		this.saveHistory(id);
		this.chekInfo(id);
	},
	mounted() {}
};
</script>

<style lang="scss">
.card-pages {
	width: 750rpx;
	display: flex;
	flex-direction: column;
	position: relative;
	overflow: hidden;
	.card-pages-user {
		padding-left: 20px;
		width: 90%;
		height: 40px;
		// background-color: red;
		position: fixed;
		top: 140rpx;
		left: 0%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		z-index: 1;
		background-color: #fff;
	}
	.follow {
		background: none;
		border: 0;
		box-sizing: border-box;
		padding: 5px 10px;
		background-color: rgba(14, 183, 218, 1);
		box-shadow: inset 0 0 0 2px rgba(14, 183, 218, 1);
		color: #fff;
		font-size: 12px;
		font-weight: 700;
		position: relative;
		vertical-align: middle;
	}
	.followed {
		background: none;
		border: 0;
		box-sizing: border-box;
		padding: 5px 10px;
		box-shadow: inset 0 0 0 2px rgba(14, 183, 218, 1);
		color: #0eb7da;
		font-size: 12px;
		font-weight: 700;
		position: relative;
		vertical-align: middle;
		box-shadow: inset 0 0 0 2px rgba(14, 183, 218, 0.4);
	}
	.card-img-swiper {
		margin: 30rpx auto;
		width: 684rpx;
		margin-top: 230rpx;
	}
	.card-info {
		width: 100%;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		.card-title {
			width: 90%;
			font-family: 'Source Han Sans SC Bold';
			font-size: 18px;
		}
		.card-content {
			width: 90%;
			font-size: 14px;
			letter-spacing: 1.1px;
			.labels {
				margin-left: 15px;
				font-size: 12px;
				span {
					margin-left: 10rpx;
				}
			}
		}
		.card-times {
			width: 100%;
			span {
				margin-left: 50rpx;
				font-size: 12px;
				color: #666;
			}
		}
	}
	.card-comment-warp {
		width: 100%;
		padding-left: 20px;
		.card-comment {
			.all-comment {
				width: 100%;
				color: #666;
				font-size: 12px;
			}
		}
		.input-comment {
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.comments {
			.loadMore {
				margin-top: 30rpx;
				width: 90%;
				height: 120px;
				color: #999;
				display: flex;
				justify-content: center;
			}
		}
	}
	.card-foot-warp {
		width: 750rpx;
		height: 50px;
		position: fixed;
		background-color: #fff;
		top: 94%;
		display: flex;
		.foot {
			width: 90%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.foot-input {
				width: 60%;
			}
			.foot-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				font-weight: 700;
				height: 100%;
				width: 60px;
				.certishare-icon {
					display: flex;
					justify-content: center;
					align-items: center;
					span {
						font-size: 30px;
					}
					.icon-text {
						font-size: 14px;
					}
				}
			}
			.collected {
				color: yellow;
			}
		}
	}
	.card-page-mask {
		width: 100%;
		height: 100%;
		z-index: 5;
		background-color: rgba(0, 0, 0, 0.5);
		position: absolute;
		top: 0%;
		left: 0%;
	}
	.float-window {
		width: 100%;
		height: 200px;
		background-color: #fff;
		position: fixed;
		left: 50%;
		transform: translate(-50%, -50%);
		z-index: 6;
		border-radius: 10px 10px 0 0;
		display: flex;
		// justify-content: center;
		align-items: center;
		flex-direction: column;
		transition: 0.5s;
		.text {
			font-size: 12px;
			color: #999;
			margin: 10px;
		}
		.share-btns {
			width: 80%;
			.share-btn {
				.share-icon {
					width: 50px;
					height: 50px;
					border-radius: 50%;
					background-color: #eee;
					display: flex;
					justify-content: center;
					align-items: center;
					i {
						span {
							font-size: 30px;
						}
					}
				}
				.share-text {
					font-size: 14px;
				}
			}
		}
		.manage-btns {
			width: 80%;
			.manage-btn {
				width: 50px;
				text-align: center;
				.manage-icon {
					width: 50px;
					height: 50px;
					border-radius: 50%;
					background-color: #eee;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-top: 10rpx;
					i {
						span {
							font-size: 30px;
						}
					}
				}
				.manage-text {
					font-size: 14px;
					margin: 10rpx;
				}
			}
		}
	}
}
</style>
